(function() {

this.pasta = this.pasta || {};
this.pasta.webgui = this.pasta.webgui || {};

this.pasta.webgui.Stage = pasta.webgui.ContainerControl.extend(
{
	init : function() 
	{
		this._super();
		
		this.cid = "stage";
		pasta.webgui._controlList[this.cid] = this;
		
		var id = this.cid; 
		
		this.canvas = document.getElementById("stage");
		this.clientCanvas = document.getElementById("stage_client");
		
		var obj = this;
		window.onresize = function() { obj.doScreenResized(); };
		
		this.canvas.onmousemove = function(e) { if (!e) e = window.event; obj.doScreenMouseMove(e.pageX, e.pageY); };
		this.canvas.onmouseup = function(e) { if (!e) e = window.event; obj.doScreenMouseUp(e.pageX, e.pageY); };
		this.canvas.onmousedown = function(e) { if (!e) e = window.event; obj.doMouse(e.pageX, e.pageY); };
		this.canvas.ontouchstart = function(e) { if (!e) e = window.event; obj.doMouse(e.pageX, e.pageY); };
		this.canvas.ontouchend = function(e) { if (!e) e = window.event; obj.doMouse(e.pageX, e.pageY); };
		
		this.mouseX = 0;
		this.mouseY = 0;
		
		this.mouseListeners = new pasta.util.ArrayList();
		this.resizeListeners = new pasta.util.ArrayList();
	},
	
	addMouseListener : function(obj)
	{
		this.mouseListeners.add(obj);
	},
	
	delMouseListener : function(obj)
	{
		this.mouseListeners.delByObject(obj);
	},
	
	addResizeListener : function(obj)
	{
		this.resizeListeners.add(obj);
	},
	
	delResizeListener : function(obj)
	{
		this.resizeListeners.delByObject(obj);
	},
	
	doMouse : function(x, y)
	{
		this.mouseX = x;
		this.mouseY = y;
	},
	
	doScreenMouseMove : function(x, y)
	{
		this.mouseX = x;
		this.mouseY = y;
		
		for (var i = 0; i < this.mouseListeners.getLength(); i++)
		{
			try
			{
				this.mouseListeners.get(i).doScreenMouseMove(x, y);
			}
			catch (e)
			{
			}
		}
	},
	
	doScreenMouseUp : function(x, y)
	{
		for (var i = 0; i < this.mouseListeners.getLength(); i++)
		{
			try
			{
				this.mouseListeners.get(i).doScreenMouseUp(x, y);
			}
			catch (e)
			{
			}
		}
	},
	
	doScreenResized : function()
	{
		for (var i = 0; i < this.resizeListeners.getLength(); i++)
		{
			try
			{
				this.resizeListeners.get(i).doScreenResized(this.getWidth(), this.getHeight);
			}
			catch (e)
			{
			}
		}
	},
	
	setBackground : function(image)
	{
		this.canvas.style.background = "url(" + image + ")";
	},
	
	getMouseX : function()
	{
		return this.mouseX;
	},
	
	getMouseY : function()
	{
		return this.mouseY;
	},
	
	getWidth : function()
	{
		return window.innerWidth;
	},
	
	getHeight : function()
	{
		return window.innerHeight;
	},
});

})();